package com.kma.ncpractice2013.model;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.Date;

/**
 *
 * @author Illya
 */
public class User implements Serializable
{
    // TODO: specify the default values for several columns in the DB
    private int id;
    private String login;
    private String password;
    private String salt;
    private String registrationToken;
    private String name;
    private String surname;
    private String phone;
    private int blocked; // 1 == true, 0 == false
    private Date birthdate; // what if forbid users to modify it?
    private Date registrationDate;
    private Group groupId;
    private AccessLevel accessLevelId;
	private String passwdResetToken;
	private double bonus;
	private double totalBonus;

	public String getPasswdResetToken()
	{
		return passwdResetToken;
	}

	public void setPasswdResetToken(String passwdResetToken)
	{
		this.passwdResetToken = passwdResetToken;
	}

	public double getBonus()
	{
		return bonus;
	}

	public void setBonus(double bonus)
	{
		this.bonus = bonus;
	}

	public double getTotalBonus()
	{
		return totalBonus;
	}

	public void setTotalBonus(double totalBonus)
	{
		this.totalBonus = totalBonus;
	}

	public enum AccessLevel
    {
        nonRegisteredCustomer, nonConfirmedCustomer, registeredCustomer,
        manager, administrator;

	    @Override
	    public String toString()
	    {
		    return this.name();
	    }

        public int getValue()
        {
            return ordinal() + 1;
        }

        public static AccessLevel getByValue(int v)
        {
            return AccessLevel.values()[v-1];
        }

    }

    public enum Group
    {
        nonRegisteredCustomer, ordinaryCustomer, VIPCustomer;

	    @Override
	    public String toString()
	    {
		    return this.name();
	    }

        public int getValue()
        {
            return ordinal() + 1;
        }

        public static Group getByValue(int v)
        {
            return Group.values()[v-1];
        }
    }

    //TODO: pass only compulsory arguments to the constructor
    public User()
    {

    }

	public void setAttr(String field, Object value)
	{
		Class<?> c = this.getClass();

		Field f = null;

		try
		{
			f = c.getDeclaredField(field);
		} catch (NoSuchFieldException e)
		{
			e.printStackTrace();
		}

		f.setAccessible(true);
		try
		{
			Class<?> fclass = f.getType();
			f.set(this, fclass.cast(value));
		} catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}


	}

    public void setRegistrationToken(String registrationToken) {
        this.registrationToken = registrationToken;
    }

    public void setBlocked(int blocked) {
        this.blocked = blocked;
    }

    public void setAccessLevelId(AccessLevel accessLevelId) {
        this.accessLevelId = accessLevelId;
    }

    public AccessLevel getAccessLevelId() {
        return accessLevelId;
    }

    public String getLogin() {
        return login;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getPassword() {
        return password;
    }

    public String getSalt() {
        return salt;
    }

    public String getPhone() {
        return phone;
    }

    public int getId() {
        return id;
    }

    public int getBlocked() {
        return blocked;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public Date getRegistrationDate() {
        return registrationDate;
    }

    public Group getGroupId() {
        return groupId;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }

    public void setGroupId(Group groupId) {
        this.groupId = groupId;
    }

    public String getRegistrationToken() {
        return registrationToken;
    }
    
}
